<?php

$startTime = microtime(true);
date_default_timezone_set('Asia/Saigon');
//session_cache_expire(2);
session_name('shop');
session_start();

//set_include_path($this->_frameworkPath . DIRECTORY_SEPARATOR . 'libs');

require_once '../../../vega.php';

//load constants
require_once 'Vega/Router/Standard.php';

$app->setupApp('shop');

require_once APP_PATH . DS . 'config/predefined.php';
//require_once APP_PATH.DS.'includes/common.php';
//Append local config
$localConfig = $app->loadConfig('local', 'localConfig');
$chargingConfig = $app->loadConfig('charging');
$app->coreConfig = array_merge_recursive($app->coreConfig, $localConfig, $chargingConfig);

if ($localConfig['webdebug']['enabled'] || isset($_REQUEST[$localConfig['webdebug']['secretkey']])) {
    require_once 'Vega/Log/WebDebug.php';
    $debugger = Vega_Log_WebDebug::getDebugger();
}

require_once 'Vega/Db/Mysql.php';
$db = new Vega_Db_Mysql($app->coreConfig['database']['master'], $app->coreConfig['database']['slave']);

require_once 'Vega/Dao.php';
Vega_Dao::setDb($db);
Vega_Dao::setLoader($app->getLoader());
Vega_Dao::setLoader(Vega_App::getInstance()->getLoader());

// Check user cookies
if (isset($_COOKIE['userid']) && isset($_COOKIE['username']) && isset($_COOKIE['userhash'])) {
    $userid = $_COOKIE['userid'];
    $username = $_COOKIE['username'];

    if (!isset($_SESSION['userid'])) {
        if (md5($_COOKIE['userid'] . $_COOKIE['username'] . 'Mobitv') == $_COOKIE['userhash']) {
            $adminDao = Vega_Dao::factory('Mobitv_Admin_Admin');
            $adminDao->tryLoginCookie($userid, $username);
        }
    }
}

$response = $app->getResponse();
//$response->startCache((isset($_SESSION['userid']) ? $_SESSION['userid'] : 0), $cacheEngine);

require_once 'Vega/View.php';
//Vega_View::setGlobalCacheManager($cacheEngine);

require_once 'Vega/Dispatcher.php';
$dispatcher = new Vega_Dispatcher($app);

try {
    $response = $dispatcher->dispatch();
} catch (Exception $e) {
    switch ($e->getCode()) {
        case 404:
            $response = $app->getResponse();
            $response->setResponseCode(404);
            header("Content-type: text/html");
            $response->setOutput(file_get_contents('static/404/index.html'));
            echo $response;
            break;
        case 401:
            $response = $app->getResponse();
            $response->setResponseCode(401);
            header("Content-type: text/html");
            $response->setOutput(file_get_contents('static/401/index.html'));
            echo $response;
            break;
        default:
            $response->setResponseCode(500);
            $response->setOutput(file_get_contents('static/offline/index.html'));
            //$response->setOutput($e->getMessage());
            echo $response;

            if (isset($localConfig['logging']['error']) && $localConfig['logging']['error']['enabled']) {
                require_once 'Vega/Log/Writer.php';
                $log = new Vega_Log_Writer($localConfig['logging']['dir'] . DS . $localConfig['logging']['error']['file']);
                $line = sprintf("%s %s", date('d-m-Y H:i:s'), $e->getMessage());
                $log->write($line);
            }
    }
    exit;
}
echo $response;

$cwdCookie = isset($localConfig['webdebug']['cookie_key']) && isset($_COOKIE[$localConfig['webdebug']['cookie_key']]);

if ($localConfig['webdebug']['enabled'] || isset($_REQUEST[$localConfig['webdebug']['secretkey']]) || $cwdCookie) {
    if ($app->getWebDebugConfig()) {
        $debugger->setConfig($app->coreConfig);
        $debugger->setTime(microtime(true) - $startTime);
        $debugger->show();
    }
}
